# Copyright (C) 2007 Laszlo Pandy <laszlok2@gmail.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

from AdventureMoney.core import ShareProfile

import InterfaceUtils

class AdvancedShareDialog:
	def __init__(self, parentWindow, parent, account):
		self.wTree = InterfaceUtils.GetGladeTree("AdvancedShareDialog")
		
		signals = {
			"OnPersonChanged"	: self.OnPersonChanged,
			"OnNoneRadioToggled"	: self.OnRadioToggled,
			"OnEqualRadioToggled"	: self.OnRadioToggled,
			"OnExactRadioToggled"	: self.OnRadioToggled,
			"OnPercentRadioToggled"	: self.OnRadioToggled,
			"OnPercentChanged"	: self.OnPercentChanged,
			"OnExactChanged"	: self.OnExactChanged,
			"OnClose"		: self.OnClose,
		}
		
		self.wTree.signal_autoconnect(signals)
		
		self.account = account
		self.parent = parent
		
		self.window = self.wTree.get_widget("AdvancedShareDialog")
		self.window.set_transient_for(parentWindow)
		self.window.set_modal(True)
		self.window.set_destroy_with_parent(True)
		
		self.mainVBox = self.wTree.get_widget("MainVBox")
		self.peopleCombo = self.wTree.get_widget("PeopleComboBox")
		self.noneSharedRadio = self.wTree.get_widget("NoneSharedRadio")
		self.equalSharedRadio = self.wTree.get_widget("EqualSharedRadio")
		self.exactSharedRadio = self.wTree.get_widget("ExactSharedRadio")
		self.percentSharedRadio = self.wTree.get_widget("PercentSharedRadio")
		self.percentScale = self.wTree.get_widget("PercentHScale")
		self.exactEntry = self.wTree.get_widget("ExactSpinButton")
		
		for person in self.account.getPeople():
			self.peopleCombo.append_text(person.getName())
			
		self.peopleCombo.set_active(0)
		
		self.window.show_all()
		
	def OnPersonChanged(self, widget):
		name = self.peopleCombo.get_active_text()
		person = self.account.findPerson(name)
		shareType, shareValue = self.parent.GetPersonShare(person)
		
		if shareType == ShareProfile.SHARE_NONE:
			self.noneSharedRadio.set_active(True)
		elif shareType == ShareProfile.SHARE_EQUAL:
			self.equalSharedRadio.set_active(True)
		elif shareType == ShareProfile.SHARE_EXACT:
			self.exactSharedRadio.set_active(True)
			self.exactEntry.set_text(str(shareValue))
		elif shareType == ShareProfile.SHARE_PERCENT:
			self.percentSharedRadio.set_active(True)
			self.percentScale.set_value(shareValue)
	
	def OnRadioToggled(self, widget):
		if not widget.get_active():
			if widget is self.exactSharedRadio:
				self.exactEntry.set_sensitive(False)
			elif widget is self.percentSharedRadio:
				self.percentScale.set_sensitive(False)
		else:
			name = self.peopleCombo.get_active_text()
			person = self.account.findPerson(name)
			if widget is self.noneSharedRadio:
				self.parent.SetPersonShare(person, ShareProfile.SHARE_NONE)
			elif widget is self.equalSharedRadio:
				self.parent.SetPersonShare(person, ShareProfile.SHARE_EQUAL)
			elif widget is self.exactSharedRadio:
				self.exactEntry.set_sensitive(True)
				value = self.exactEntry.get_text()
				self.parent.SetPersonShare(person, ShareProfile.SHARE_EXACT, value)
			elif widget is self.percentSharedRadio:
				self.percentScale.set_sensitive(True)
				value = int(self.percentScale.get_value())
				self.parent.SetPersonShare(person, ShareProfile.SHARE_PERCENT, value)
	
	def OnPercentChanged(self, widget):
		name = self.peopleCombo.get_active_text()
		value = int(self.percentScale.get_value())
		person = self.account.findPerson(name)
		self.parent.SetPersonShare(person, ShareProfile.SHARE_PERCENT, value)
	
	def OnExactChanged(self, widget):
		name = self.peopleCombo.get_active_text()
		value = self.exactEntry.get_text()
		person = self.account.findPerson(name)
		self.parent.SetPersonShare(person, ShareProfile.SHARE_EXACT, value)
		
	def OnClose(self, widget):
		self.window.destroy()
		
